import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

import vuexPersist from "vuex-persist";
export default new Vuex.Store({
  state: {
    shopcar: [
      {
        title: "ysl小金条",
        price: 300,
        num: 2,
      },
      {
        title: "雅诗兰黛持妆粉底液",
        price: 500,
        num: 1,
      },
      {
        title: "sk2神仙水",
        price: 1400,
        num: 1,
      },
      {
        title: "雅诗兰黛小棕瓶",
        price: 700,
        num: 2,
      },
    ],
  },
  mutations: {
    change() {},
    remove(state, index) {
      state.shopcar.splice(index, 1);
    },
  },
  actions: {},
  modules: {},
  getters: {
    sum(state) {
      let num = 0;
      state.shopcar.forEach((item) => {
        num += item.price * item.num;
      });
      return num;
    },
  },
  plugins: [
    new vuexPersist({
      storage: window.localStorage,
    }).plugin,
  ],
});
